package com.weng.platform.resource.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.cloud.utils.CloudAuthUtil;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.resource.entity.Resource;
import com.weng.platform.resource.model.vo.ResourceVo;
import com.weng.platform.resource.service.IResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;


/**
 * 资源信息 API实现
 *
 * @author weng
 * @date 2021-03-01 16:29:08
 *
 */  
@Slf4j
@RestController
@RequestMapping("/resources")
@Api(value = "资源信息api", tags = {"资源信息操作接口"})
public class ResourceApi extends BaseController {
    @Autowired
    private IResourceService resourceService;

    /**
     * 功能描述:单个获取资源信息
     */
    @ApiOperation(value = "获取资源信息", notes = "")
    @GetMapping(value = "/{id}")
    public ResponseData get(@PathVariable("id") String id){
        Resource resource = resourceService.getById(id);
        return success(resource);
    }


    /**
     * 条件 分页查询资源信息
     */
    @ApiOperation(value = "分页获取资源信息", notes = "")
    @GetMapping(value = "")
    public ResponseData list(Integer pageNo,Integer pageSize, HttpServletRequest request,Boolean isQueryChild, String columnId, String name){
			String ownerId = CloudAuthUtil.getCurUserId(request);
			columnId = StringUtils.isEmpty(columnId) ? "-1" : columnId;
			IPage<Resource> iPage = resourceService.list(pageNo, pageSize, ownerId, isQueryChild, columnId, name);
        return success(iPage);
    }


    /**
	 * 新增资源信息
	 */
	@ApiOperation(value = "新增资源信息", notes = "")
	@PostMapping(value = "")
	public ResponseData add(@Valid @RequestBody ResourceVo resourceReq,  HttpServletRequest request){
		Resource resource = Convert.convert(Resource.class, resourceReq);
		resource.setOwnerId( CloudAuthUtil.getCurUserId(request));
		resource.setOwnerName(CloudAuthUtil.getCurUserTrueName(request));
		resourceService.add(resource);
		return success();
	}

	/**
	 * 更新资源信息
	 */
	@ApiOperation(value = "更新资源信息", notes = "")
	@PutMapping(value = "")
	public ResponseData update(@Valid @RequestBody ResourceVo resourceReq){
		Resource resource = Convert.convert(Resource.class, resourceReq);
		resourceService.update(resource);
		return success();
	}


	/**
	 * 删除资源信息
	 */
	@ApiOperation(value = "ids 删除资源信息", notes = "可以删除多个，中间以,号分隔")
	@DeleteMapping(value = "")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		resourceService.removeByIds(Arrays.asList(ids.split(",")));
		return success(deleteReq.getIds());
	}
}
